clear
set more off

global lecind    "\\intra\partages\au_amic2\SRCV\TABLES_INITIALES\INPUT\INDIVIDUS"
global lecindsas "\\intra\partages\au_amic2\SRCV_AVRIL2021\TABLES_INITIALES_AVRIL2021\INDIVIDUS"
global ecrind    "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\INDIVIDUS"


/* 1 - EXTRACTION DES VARIABLES SELECTIONNéES */
/* a - fichier STATA 1ère livraison 2008-2017 */
foreach an of numlist 8  9 {
cd "$lecind"
use individus0`an'_diff.dta, clear
keep 	ind men age adultvrairep idmenc_* idindc_* pond_0`an' pb040 ///
		py010n py050n py100n py090n py110n py130n py120n py140n ///
		rb030 
gen annee_SRCV = 200`an'
order men ind annee_SRCV adultvrairep 
cd "$ecrind"
save indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 10 / 11 {
cd "$lecind"
use individus`an'_diff.dta, clear
keep 	ind men age adultvrairep idmenc_* idindc_* pond_`an' pb040 ///
		py010n py050n py100n py090n py110n py130n py120n py140n ///
		rb030 
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
cd "$ecrind"
save indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 12 / 13 {
cd "$lecind"
use individus`an'_diffv2.dta, clear
keep 	ind men age adultvrairep idmenc_* idindc_* pond_`an' pb040 ///
		py010n py050n py100n py090n py110n py130n py120n py140n ///
		rb030 
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
cd "$ecrind"
save indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 14/16  {
cd "$lecind"
use individus`an'_diff.dta, clear
keep 	ind men age adultvrairep idmenc_* idindc_* pond_`an' pb040 ///
		py010n py050n py100n py090n py110n py130n py120n py140n ///
		rb030 
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
cd "$ecrind"
save indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 17  {
cd "$lecind"
use individus`an'_diff.dta, clear
keep 	ind men age adultvrairep idmenc_* idindc_* pond_`an' pb040 ///
		py010n py050n py100n py090n py110n py130n py120n py140n ///
		rb030 rb040 
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
cd "$ecrind"
save indiv_`an'.dta, replace
display `an'
count 
}

/* b - fichier SAS 2nde livraison 2018-2019 */
foreach an of numlist 18 / 19 {
cd "$lecindsas"
use individus_20`an'.dta, clear
keep 	ind men age adultvrairep IDMENC_* IDINDC_* POND_`an' PB040 ///
		PY010N PY050N PY100N PY090N PY110N PY130N PY120N PY140N ///
		RB030 RB040
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
cd "$ecrind"
save indiv_`an'.dta, replace
display `an'
count 
}

/* 2 - CHANGEMENT DE NOM DES VARIABLES */
foreach an of numlist 8 / 17 {
cd "$ecrind"
use indiv_`an'.dta, clear
rename py100n PY100N
rename py110n PY110N
rename py010n PY010N
rename py050n PY050N
rename py090n PY090N
rename py120n PY120N
rename py130n PY130N
rename py140n PY140N
rename idmenc_* idmenc
rename idindc_* idindc
rename pond_* pond_ind
save indiv_`an'.dta, replace
}

foreach an of numlist 18 / 19 {
cd "$ecrind"
use indiv_`an'.dta, clear
rename IDMENC_* idmenc
rename IDINDC_* idindc
rename POND_* pond_ind
rename PB040 pb040
rename RB030 rb030
rename RB040 rb040
save indiv_`an'.dta, replace
}


/* 3 - EMPILEMENT DES TABLES */
clear 
use "$ecrind\indiv_8.dta", clear
append using 	"$ecrind\indiv_9.dta" "$ecrind\indiv_10.dta" "$ecrind\indiv_11.dta" ///
				"$ecrind\indiv_12.dta" "$ecrind\indiv_13.dta" "$ecrind\indiv_14.dta" ///
				"$ecrind\indiv_15.dta" "$ecrind\indiv_16.dta" "$ecrind\indiv_17.dta" ///
				"$ecrind\indiv_18.dta" "$ecrind\indiv_19.dta" 
count /* 316 272 + 19 var. */
order idmenc annee_SRCV ind men
sort idmenc annee_SRCV ind men				
save "$ecrind\individus_2008_2019.dta", replace


/* 4 - LABELS */
clear 
use "$ecrind\individus_2008_2019.dta", clear
label variable ind				"Identifiant longitudinal de l individu"
label variable men 				"Identifiant longitudinal du menage" 
label variable annee_SRCV		"annee de l'enquete/du fichier"
label variable age				"Age de l individu a la date de l enquete"
label variable adultvrairep		"L individu a rempli un questionnaire individuel"
label variable idmenc			"Identifiant transversal du menage"
label variable idindc			"Identifiant transversal de l individu"
label variable pond_ind			"Ponderation transversale individus"
label variable pb040			"Ponderation personnelle transversale européenne"
label variable PY010N			"Salaire net percu"
label variable PY050N			"Benef/pertes nets especes activ indep"
label variable PY100N			"Montant allocations vieillesse nettes"
label variable PY090N			"Montant net allocations chomage"
label variable PY110N			"Allocations de reversion nettes"
label variable PY130N			"Pensions d'invalidite nettes"
label variable PY120N			"Indemnites de maladie nettes"
label variable PY140N			"Bourses d etudes nettes"
label variable rb030			"Identifiant individuel européen"
label variable rb040			"Identifiant ménage européen longitudinal - table individu"
save "$ecrind\individus_2008_2019.dta", replace
tab age, missing
clear 
use "$ecrind\individus_2008_2019.dta", clear
